//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdlib.h>
#include <printers.hpp>
#include <Winspool.h>
#include <Wingdi.h>
#pragma hdrstop

#include "uStyles.h"
#include "FenMain_Etat.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CCheckB"
#pragma link "CComboBx"
#pragma link "CEdit"
#pragma link "CGBox"
#pragma link "CRadBtn"
#pragma link "CBitBtn"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
#define MODULE_TAG  MODULE_TAG_STYLES
//---------------------------------------------------------------------------
#define COLOR_GRIS_PERSO   "ColorA=DEDEDE"
//---------------------------------------------------------------------------
void __fastcall TfrmStyles::InitEcranQuandHorsGenetat( void)
{
  try
  {
    eNomStyle->ReadOnly = true;
    btnInserer->Visible = false; btnSupprimer->Visible = false;
    pnl2->Visible       = false;
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
void __fastcall TfrmStyles::VisibiliteDesElementsDuStyle( void)
{
  try
  {
    if( !Glob->LanceEnConception)
    {
      pnPresentation->Align = alNone;
      pnStyleFond->Align    = alNone;
      pnCouleurFond->Align  = alNone;
      pnPolice->Align       = alNone;
      pnNomStyle->Align     = alNone;

      // Nom du Style
      pnNomStyle->Visible = !(eNomStyle->Text).IsEmpty();
      // Police du Style
      chkPolice->Visible = false;
      pnPolice->Visible  = chkPolice->Checked;
      chkNomPoliceModifiable->Enabled  = false;
      chkTailleFontModifiable->Enabled = false;
      // Couleur du fond du Style
      chkCouleurFond->Visible = false;
      pnCouleurFond->Visible  = chkCouleurFond->Checked;
      // Style du fond du Style
      chkStyleFond->Visible = false;
      pnStyleFond->Visible  = chkStyleFond->Checked;
      chkStyleFondModifiable->Enabled  = false;
      cbStyleFond->Enabled = chkStyleFondModifiable->Checked;
      // Presentation
      chkPresentation->Visible = false;
      pnPresentation->Visible  = chkPresentation->Checked;

      pnNomStyle->Align     = alTop;
      pnPolice->Align       = alTop;
      pnCouleurFond->Align  = alTop;
      pnStyleFond->Align    = alTop;
      pnPresentation->Align = alTop;
    }
    else
    {
      // Police du Style
      gbPolice->Enabled = chkPolice->Checked;
      if( !gbPolice->Enabled)
      {
        chkNomPoliceModifiable->Checked  = false;
        chkTailleFontModifiable->Checked = false;
      }
      // Couleur du fond du Style
      btCouleurFond->Enabled = chkCouleurFond->Checked;
      // Style du fond du Style
      gbStyleFond->Enabled = chkStyleFond->Checked;
      if( !gbStyleFond->Enabled)
        chkStyleFondModifiable->Checked = false;
      // Presentation
      gbPresentation->Enabled = chkPresentation->Checked;
      if( !gbPresentation->Enabled)
      {
        rbGauche->Checked = false;
        rbCentre->Checked = false;
        rbDroite->Checked = false;
      }
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
void __fastcall TfrmStyles::AjoutColorPerso( const AnsiString &asLstColorPerso)
{
  AnsiString asTmp,_asLstColorPerso(asLstColorPerso);
  try
  {
    ColorDialog1->CustomColors->Clear();
    if( _asLstColorPerso.IsEmpty())
      ColorDialog1->CustomColors->Add(COLOR_GRIS_PERSO);
    else
    {
      while( !_asLstColorPerso.IsEmpty())
      {
        asTmp = _asLstColorPerso.SubString(_asLstColorPerso.LastDelimiter(";") + 1, _asLstColorPerso.Length());
        ColorDialog1->CustomColors->Add(asTmp);
        if( _asLstColorPerso.LastDelimiter(";"))
             _asLstColorPerso = _asLstColorPerso.SubString(1, _asLstColorPerso.LastDelimiter(";") - 1);
        else _asLstColorPerso = EmptyStr;
      }
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
void __fastcall TfrmStyles::InitEcran( void)
{
  EICStyle *StyleSelect;
  try
  {
    if( lstStyles->ItemIndex != -1)
    {
      StyleSelect = (EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex];
      // Nom du Style
      eNomStyle->Enabled       = true; eNomStyle->Text          = StyleSelect->Nom;
      // Police du Style
      chkPolice->Enabled       = true; chkPolice->Checked       = StyleSelect->Font;
      if( StyleSelect->Font) pnApercuPolice->Font->Assign((TFont *)StyleSelect->Font);
      chkNomPoliceModifiable->Checked  = StyleSelect->NomPoliceModifiable;
      chkTailleFontModifiable->Checked = StyleSelect->TailleFontModifiable;
      // Couleur du fond du Style
      chkCouleurFond->Enabled  = true; chkCouleurFond->Checked  = (StyleSelect->BrushColor   != Nocl);
      if( StyleSelect->BrushColor!= Nocl) pnApercuCoulFd->Color =  StyleSelect->BrushColor;
      AjoutColorPerso( StyleSelect->LstColorPerso);
      // Style du fond du Style
      chkStyleFond->Enabled    = true; chkStyleFond->Checked    = (StyleSelect->BrushStyle   != Nobs);
      if( !chkStyleFond->Checked) cbStyleFond->ItemIndex = -1;
      else                        cbStyleFond->ItemIndex = (int)((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushStyle;
      chkStyleFondModifiable->Checked  = StyleSelect->StyleFondModifiable;
      // Presentation
      chkPresentation->Enabled = true; chkPresentation->Checked = (StyleSelect->Presentation & (Tx_HGauche | Tx_HCentre | Tx_HDroite));
      if( chkPresentation->Checked)
      {
        switch( StyleSelect->Presentation)
        {
          case Tx_HGauche : rbGauche->Checked = true; break;
          case Tx_HCentre : rbCentre->Checked = true; break;
          case Tx_HDroite : rbDroite->Checked = true; break;
          default         : ASSERT(false);
        }
      }

      btnSupprimer->Enabled = true;
    }
    else
    {
      // Nom du Style
      eNomStyle->Enabled       = false; eNomStyle->Text = EmptyStr;
      // Police du Style
      chkPolice->Enabled       = false; chkPolice->Checked       = false; chkNomPoliceModifiable->Checked = false; chkTailleFontModifiable->Checked = false;
      // Couleur du fond du Style
      chkCouleurFond->Enabled  = false; chkCouleurFond->Checked  = false;
      // Style du fond du Style
      chkStyleFond->Enabled    = false; chkStyleFond->Checked    = false; chkStyleFondModifiable->Checked = false;
      // Presentation
      chkPresentation->Enabled = false; chkPresentation->Checked = false;
      btnSupprimer->Enabled = false;
    }
    VisibiliteDesElementsDuStyle();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmStyles::EcranIsInit( bool bInit)
{
  try
  {
    gpb->Visible  = bInit;
    pnl3->Visible = bInit;
    pnl2->Visible = bInit;
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------
void __fastcall TfrmStyles::InitStylesAvecDoc( TDocument *pDoc)
{
  try
  {
    pDocCourant        = pDoc;
    pLstStylesCourante = NULL;
    lstStyles->Items->Assign( pDoc->Styles);
    EcranIsInit(true);
    if( !Glob->LanceEnConception) InitEcranQuandHorsGenetat();
    lstStyles->ItemIndex = 0;
    InitEcran();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------
void __fastcall TfrmStyles::InitStylesAvecListeStyles( EICListStyle *pLstStyles)
{
  try
  {
    pDocCourant        = NULL;
    pLstStylesCourante = pLstStyles;
    lstStyles->Items->Assign( pLstStyles);
    EcranIsInit(true);
    if( !Glob->LanceEnConception) InitEcranQuandHorsGenetat();
    lstStyles->ItemIndex = 0;
    InitEcran();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------
__fastcall TfrmStyles::TfrmStyles(TComponent* Owner) : CEICForm(Owner)
{
  // Au cas où la fenêtre des styles s'afficherait sans avoir été initialisée par l'une des 2 fonctions ci-dessus
  EcranIsInit(false);
}
//---------------------------------------------------------------------------
CEICForm *__export TfrmStyles::CreateInstance(TComponent *Owner)
{
  try { return new TfrmStyles(Owner); }
  catch (...) { return NULL; }
}
//---------------------------------------------------------------------------
__fastcall TfrmStyles::~TfrmStyles()
{
  try
  {
    if( pDocCourant)
    {
      pDocCourant->Styles->Clear();
      if( lstStyles->Items->Count != 0)
      {
        for( int i = 0; i < lstStyles->Items->Count; i++)
        {
          lstStyles->ItemIndex = i;
          pDocCourant->Styles->AddObject( lstStyles->Items->Strings[lstStyles->ItemIndex],(EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex]);
        }
      }
    }
    if( pLstStylesCourante)
    {
      pLstStylesCourante->Clear();
      if( lstStyles->Items->Count != 0)
      {
        for( int i = 0; i < lstStyles->Items->Count; i++)
        {
          lstStyles->ItemIndex = i;
          pLstStylesCourante->AddObject( lstStyles->Items->Strings[lstStyles->ItemIndex],(EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex]);
        }
      }
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------  

void __fastcall TfrmStyles::lstStylesClick(TObject *Sender)
{
  InitEcran();
}
//---------------------------------------------------------------------------

void __fastcall TfrmStyles::eNomStyleExit(TObject *Sender)
{
  try
  {
                 lstStyles->Items->Strings[lstStyles->ItemIndex]       = eNomStyle->Text;
    ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Nom = eNomStyle->Text;

    if( pDocCourant) pDocCourant->Modifie();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmStyles::chkPoliceCheck(TObject *Sender, bool bIsChecked)
{
  try
  {
    if( ((CCheckBox *)Sender)->Enabled)
    {
      switch( ((CCheckBox *)Sender)->Tag)
      {
        case 1  : gbPolice->Enabled = bIsChecked;
                  if( !bIsChecked && ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font)
                    ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font = NULL;
                  else if( bIsChecked && !((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font)
                  {
                    ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font = new EICFont();
                    FontDialog1->Font->Assign(((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font);
                  }
                  break;
        case 2  : btCouleurFond->Enabled = bIsChecked;
                  if( !bIsChecked) ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushColor = Nocl;
                  else if( ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushColor == Nocl)
                    ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushColor = clWhite; // Couleur par défaut = Blanc
                  break;
        case 3  : gbStyleFond->Enabled = bIsChecked;
                  if( !bIsChecked)
                  {
                    ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushStyle = Nobs;
                    cbStyleFond->ItemIndex = -1;
                  }
                  else
                  {
                    if( ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushStyle == Nobs)
                    {
                      cbStyleFond->ItemIndex = bsClear;
                      ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushStyle = (TBrushStyle)cbStyleFond->ItemIndex;
                    }
                    else cbStyleFond->ItemIndex = (int)((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushStyle;
                  }
                  cbStyleFond->Enabled = bIsChecked;
                  break;
        case 4  : gbPresentation->Enabled = bIsChecked;
                  if( !bIsChecked)
                  {
                    ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Presentation = Tx_Aucun;
                    rbGauche->Checked = false;
                    rbCentre->Checked = false;
                    rbDroite->Checked = false;
                  }
                  else
                  {
                    if( ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Presentation == Tx_Aucun)
                    {
                      ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Presentation = Tx_HGauche; // Alignement par défaut = à Gauche
                      rbGauche->Checked = true;
                    }
                    else
                    {
                      switch( ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Presentation)
                      {
                        case Tx_HGauche : rbGauche->Checked = true; break;
                        case Tx_HCentre : rbCentre->Checked = true; break;
                        case Tx_HDroite : rbDroite->Checked = true; break;
                        default         : ASSERT(false);
                      }
                    }
                  }
                  break;
        default : ASSERT(false);
      }
      if( pDocCourant) pDocCourant->Modifie();
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------  
void __fastcall TfrmStyles::chkTailleFontModifiableCheck(TObject *Sender, bool bIsChecked)
{
  try
  {
    if( (lstStyles->ItemIndex != -1) && Glob->LanceEnConception)
    {
      switch( ((CCheckBox *)Sender)->Tag)
      {
        case 10 : ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->TailleFontModifiable = bIsChecked; break;
        case 11 : ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->StyleFondModifiable  = bIsChecked; break;
        case 12 : ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->NomPoliceModifiable  = bIsChecked; break;
        default : ASSERT(false);
      }
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//--------------------------------------------------------------------------- 
void __fastcall TfrmStyles::btPoliceClick(TObject *Sender)
{
  int SizeFixed;
  try
  {
    // Si la police n'est pas encore initialisée, on la crée
    if( !((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font)
      ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font = new EICFont();
    // Sauvegarde de la police
    FontDialog1->Font->Assign(((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font);
    if( FontDialog1->Execute( ))
    {
      if( !((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->TailleFontModifiable)
        SizeFixed = ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font->Size;
      ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font->Assign( FontDialog1->Font);
      if( !((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->TailleFontModifiable)
        ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font->Size = SizeFixed;
      pnApercuPolice->Font->Assign((TFont *)(((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Font));
    }

    if( pDocCourant) pDocCourant->Modifie();
   }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------

AnsiString __fastcall TfrmStyles::RecupColorPerso( void)
{
  AnsiString asRetour = EmptyStr;
  try
  {
    for( int i = 0; i < ColorDialog1->CustomColors->Count; i++)
    {
      if( !(ColorDialog1->CustomColors->Strings[i]).AnsiPos("=FFFFFFFF"))
      {
        if( !asRetour.IsEmpty()) asRetour += ";";
        asRetour += ColorDialog1->CustomColors->Strings[i];
      }
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
  return asRetour;
}
//---------------------------------------------------------------------------
void __fastcall TfrmStyles::btCouleurFondClick(TObject *Sender)
{
  try
  {
    ColorDialog1->Color = ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushColor;
    if( ColorDialog1->Execute())
    {
      ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushColor    = ColorDialog1->Color;
      pnApercuCoulFd->Color = ColorDialog1->Color;
      ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->LstColorPerso = RecupColorPerso();
    }

    if( pDocCourant) pDocCourant->Modifie();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmStyles::cbStyleFondChange(TObject *Sender)
{
  try
  {
    ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->BrushStyle = (TBrushStyle)cbStyleFond->ItemIndex;

    if( pDocCourant) pDocCourant->Modifie();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }  
}
//---------------------------------------------------------------------------  
void __fastcall TfrmStyles::cbStyleFondDrawItem(TWinControl *Control, int Index,
                                                const TRect &Rect, TOwnerDrawState State)
{
  try
  {
    ((CComboBox*)Control)->Canvas->Brush->Color = clWindow;
    ((CComboBox*)Control)->Canvas->Brush->Style = bsSolid;
    ((CComboBox*)Control)->Canvas->FillRect(Rect);
    ((CComboBox*)Control)->Canvas->Brush->Color = clBlack;
    ((CComboBox*)Control)->Canvas->Brush->Style = (TBrushStyle)Index;
    ((CComboBox*)Control)->Canvas->Pen->Color = clHighlight;
    ((CComboBox*)Control)->Canvas->Pen->Width = 2;
    ((CComboBox*)Control)->Canvas->Pen->Style = State.Contains( odSelected)?psSolid:psClear;
    ((CComboBox*)Control)->Canvas->Rectangle(Rect.Left+1, Rect.Top+1, Rect.Right-1, Rect.Bottom-1);

    if( State.Contains( odSelected))
    {
        ((CComboBox*)Control)->Canvas->Brush->Style = bsClear;
        ((CComboBox*)Control)->Canvas->Pen->Style = psSolid;
        ((CComboBox*)Control)->Canvas->Pen->Color = clWindow;
        ((CComboBox*)Control)->Canvas->Pen->Width = 1;
        ((CComboBox*)Control)->Canvas->Rectangle(Rect.Left+2, Rect.Top+2, Rect.Right-3, Rect.Bottom-3);
    }
    if( Index <= 1)
    {
        ((CComboBox *)Control)->Canvas->Font->Color = (Index==0)?clWhite:clBlack;
        RECT rc = RECT( Rect);
        ::DrawText( ((CComboBox *)Control)->Canvas->Handle,
                    cbStyleFond->Items->Strings[Index].c_str(), cbStyleFond->Items->Strings[Index].Length(),
                    &rc, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmStyles::rbGaucheCheck(TObject *Sender, bool bIsChecked)
{
  try
  {
    if( (lstStyles->ItemIndex != -1) && bIsChecked)
    {
      switch( ((CRadioButton *)Sender)->Tag)
      {
        case 20 : ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Presentation = Tx_HGauche; break;
        case 21 : ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Presentation = Tx_HCentre; break;
        case 22 : ((EICStyle *)lstStyles->Items->Objects[lstStyles->ItemIndex])->Presentation = Tx_HDroite; break;
        default : ASSERT(false);
      }
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmStyles::FontDialog1Show(TObject *Sender)
{
  try
  {
    if( !Glob->LanceEnConception)
    {
      if( !chkNomPoliceModifiable->Checked)
        ::EnableWindow( GetDlgItem( FontDialog1->Handle, 0x470), false);
      if( !chkTailleFontModifiable->Checked)
        ::EnableWindow( GetDlgItem( FontDialog1->Handle, 0x472), false);
    }
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmStyles::btnInsererClick(TObject *Sender)
{
  EICStyle *StyleAInserer;
  try
  {
    StyleAInserer = new EICStyle();
    lstStyles->ItemIndex = lstStyles->Items->AddObject( EmptyStr, StyleAInserer);
    InitEcran();
    eNomStyle->SetFocus();

    if( pDocCourant) pDocCourant->Modifie();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }  
}
//---------------------------------------------------------------------------
void __fastcall TfrmStyles::btnSupprimerClick(TObject *Sender)
{
  try
  {
    lstStyles->Items->Delete( lstStyles->ItemIndex);
    lstStyles->ItemIndex = 0;
    InitEcran();

    if( pDocCourant) pDocCourant->Modifie();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
}
//---------------------------------------------------------------------------

void __export __fastcall AfficheStyles( TComponent* Owner, EICListStyle *pLstStyles)
{
  TfrmStyles *frmStyles = NULL;
  try
  {
    frmStyles = new TfrmStyles( Owner);
    frmStyles->InitStylesAvecListeStyles( pLstStyles);
    frmStyles->ShowModal();
  }
  catch( Exception &e)
  {
    EICDisplayException( TAGL, e);
  }
  delete frmStyles;
}
//---------------------------------------------------------------------------
void __fastcall TfrmStyles::FormActivate(TObject *Sender)
{
  if( FenMainAssistant && FenMainAssistant->Tag)
    InitStylesAvecListeStyles( *((EICListStyle**)(FenMainAssistant->Tag)) );
}
//---------------------------------------------------------------------------

